//
//  viewViewController.m
//  Project
//
//  Created by iD Student on 7/30/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "viewViewController.h"

@interface viewViewController ()

@end

@implementation viewViewController
@synthesize warning;
@synthesize woodCount;
@synthesize stoneCount;
@synthesize ironCount;
@synthesize victories;
@synthesize raidButton;
@synthesize warButton;
@synthesize attackInfo;
@synthesize attackScroll;
@synthesize attackButton;
@synthesize buildController;
@synthesize researchController;
@synthesize recruitController;
@synthesize title1Controller;
@synthesize title2Controller;
@synthesize title3Controller;
@synthesize controller;
@synthesize dayLabel;
@synthesize combatController;

- (void)viewDidLoad
{
    timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target : self selector : @selector (resource) userInfo : nil repeats : YES];
    [super viewDidLoad];
    dayLabel.text = [NSString stringWithFormat:@"%d", day];
    attackInfo.hidden = TRUE;
    attackScroll.hidden = TRUE;
    attackButton.hidden = TRUE;
    
	// Do any additional setup after loading the view, typically from a nib.
}

- (void) resource
{
    woodCount.text = [NSString stringWithFormat: @"%d", wood];
    stoneCount.text = [NSString stringWithFormat: @"%d", stone];
    ironCount.text = [NSString stringWithFormat: @"%d", iron];
    victories.text = [NSString stringWithFormat: @"%d", victor];
}

- (void)viewDidUnload  
{
    [self setDayLabel:nil];
    [self setWarning:nil];
    [self setWoodCount:nil];
    [self setStoneCount:nil];
    [self setIronCount:nil];
    [self setVictories:nil];
    [self setRaidButton:nil];
    [self setWarButton:nil];
    [self setAttackInfo:nil];
    [self setAttackScroll:nil];
    [self setAttackButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)build:(id)sender 
{
    buildController = [self.storyboard instantiateViewControllerWithIdentifier:@"buildController"];
    [self presentModalViewController: buildController animated:YES];
    
    buildController.woodCount.text = [NSString stringWithFormat:@"%d", wood];
    buildController.stoneCount.text = [NSString stringWithFormat:@"%d", stone];
    buildController.ironCount.text = [NSString stringWithFormat:@"%d", iron];
}

- (IBAction)research:(id)sender 
{
    researchController = [self.storyboard instantiateViewControllerWithIdentifier:@"researchController"];
    [self presentModalViewController: researchController animated:YES];
}

- (IBAction)recruit:(id)sender 
{
    recruitController = [self.storyboard instantiateViewControllerWithIdentifier:@"recruitController"];
    [self presentModalViewController: recruitController animated:YES];
    
    recruitController.woodCount.text = [NSString stringWithFormat:@"%d", wood];
    recruitController.stoneCount.text = [NSString stringWithFormat:@"%d", stone];
    recruitController.ironCount.text = [NSString stringWithFormat:@"%d", iron];
}

- (IBAction)nextday:(id)sender 
{
    day++;
    raidButton.hidden = FALSE;
    warButton.hidden = FALSE;
    dayLabel.text = [NSString stringWithFormat:@"%d", day];
    if (fastGathering == TRUE)
    {
        //research fastGathering, need balance
        wood = wood + (4 * woodLvl * woodLvl);
        stone = stone + (4 * stoneLvl * stoneLvl);
        iron = iron + (4 * ironLvl * stoneLvl);
    }
    else 
    {
        wood = wood + (2 * stoneLvl * woodLvl);
        stone = stone + (2 * stoneLvl * stoneLvl);
        iron = iron + (2 * ironLvl * ironLvl);
    }
    
    woodCount.text = [NSString stringWithFormat: @"%d", wood];
    stoneCount.text = [NSString stringWithFormat: @"%d", stone];
    ironCount.text = [NSString stringWithFormat: @"%d", iron];
    
    if (willBeAttacked == TRUE)
    {
        //combat text
        combatController = [self.storyboard instantiateViewControllerWithIdentifier:@"combatController"];
        [self presentModalViewController: combatController animated:YES];
        willBeAttacked = FALSE;
        warning.text = @"";
    }
    else
    {
        y = arc4random() %(100)-1;
        if ((y > 90 && day > 8) || (day > 8 && day < 56 && y > (93 - (day / 2))))
        {
            willBeAttacked = TRUE;
            attackInfo.hidden = FALSE;
            attackScroll.hidden = FALSE;
            attackButton.hidden = FALSE;
            warning.hidden = FALSE;
            attackInfo.text = @"Warning, attack incoming! Build up your defences, you are in for a fight! Our scouts estimate the combined strength of the enemy forces is:";
            estimate = (float)((12 * (3 + (day / 3))) + (4 * (2 + (day / 4))) + (22 * (1 + (day / 15))) + (54 * (0.5 + (day / 25) + (day / 25)))) * 0.9;
            warning.text = [NSString stringWithFormat: @"%.0f", estimate];
        }
    }
    
    NSUserDefaults * woodW1 = [NSUserDefaults standardUserDefaults];
    [woodW1 setInteger: woodW forKey: @"woodW"];
    [woodW1 synchronize];
    NSUserDefaults * woodS1 = [NSUserDefaults standardUserDefaults];
    [woodS1 setInteger: woodS forKey: @"woodS"];
    [woodS1 synchronize];
    NSUserDefaults * woodI1 = [NSUserDefaults standardUserDefaults];
    [woodI1 setInteger: woodI forKey: @"woodI"];
    [woodI1 synchronize];
    NSUserDefaults * stoneW1 = [NSUserDefaults standardUserDefaults];
    [stoneW1 setInteger: stoneW forKey: @"stoneW"];
    [stoneW1 synchronize];
    NSUserDefaults * stoneS1 = [NSUserDefaults standardUserDefaults];
    [stoneS1 setInteger: stoneS forKey: @"stoneS"];
    [stoneS1 synchronize];
    NSUserDefaults * stoneI1 = [NSUserDefaults standardUserDefaults];
    [stoneI1 setInteger: stoneI forKey: @"stoneI"];
    [stoneI1 synchronize];
    NSUserDefaults * ironW1 = [NSUserDefaults standardUserDefaults];
    [ironW1 setInteger: ironW forKey: @"ironW"];
    [ironW1 synchronize];
    NSUserDefaults * ironS1 = [NSUserDefaults standardUserDefaults];
    [ironS1 setInteger: ironS forKey: @"ironS"];
    [ironS1 synchronize];
    NSUserDefaults * ironI1 = [NSUserDefaults standardUserDefaults];
    [ironI1 setInteger: ironI forKey: @"ironI"];
    [ironI1 synchronize];
    NSUserDefaults * woodLvl1 = [NSUserDefaults standardUserDefaults];
    [woodLvl1 setInteger: woodLvl forKey: @"woodLvl"];
    [woodLvl1 synchronize];
    NSUserDefaults * stoneLvl1 = [NSUserDefaults standardUserDefaults];
    [stoneLvl1 setInteger: stoneLvl forKey: @"stoneLvl"];
    [stoneLvl1 synchronize];
    NSUserDefaults * ironLvl1 = [NSUserDefaults standardUserDefaults];
    [ironLvl1 setInteger: ironLvl forKey: @"ironLvl"];
    [ironLvl1 synchronize];
    NSUserDefaults * w1 = [NSUserDefaults standardUserDefaults];
    [w1 setInteger: w forKey: @"w"];
    [w1 synchronize];
    NSUserDefaults * s1 = [NSUserDefaults standardUserDefaults];
    [s1 setInteger: s forKey: @"s"];
    [s1 synchronize];
    NSUserDefaults * i1 = [NSUserDefaults standardUserDefaults];
    [i1 setInteger: i forKey: @"i"];
    [i1 synchronize];
    NSUserDefaults * x1 = [NSUserDefaults standardUserDefaults];
    [x1 setInteger: x forKey: @"x"];
    [x1 synchronize];
    NSUserDefaults * z1 = [NSUserDefaults standardUserDefaults];
    [z1 setInteger: z forKey: @"z"];
    [z1 synchronize];
    NSUserDefaults * victor1 = [NSUserDefaults standardUserDefaults];
    [victor1 setInteger: victor forKey: @"victor"];
    [victor1 synchronize];    
    NSUserDefaults * wood1 = [NSUserDefaults standardUserDefaults];
    [wood1 setInteger: wood forKey: @"wood"];
    [wood1 synchronize]; 
    NSUserDefaults * stone1 = [NSUserDefaults standardUserDefaults];
    [stone1 setInteger: stone forKey: @"stone"];
    [stone1 synchronize]; 
    NSUserDefaults * iron1 = [NSUserDefaults standardUserDefaults];
    [iron1 setInteger: iron forKey: @"iron"];
    [iron1 synchronize]; 
    NSUserDefaults * day1 = [NSUserDefaults standardUserDefaults];
    [day1 setInteger: day forKey: @"day"];
    [day1 synchronize]; 
    NSUserDefaults * fastGathering1 = [NSUserDefaults standardUserDefaults];
    [fastGathering1 setInteger: fastGathering forKey: @"fastGathering"];
    [fastGathering1 synchronize]; 
    NSUserDefaults * cheapTroops1 = [NSUserDefaults standardUserDefaults];
    [cheapTroops1 setInteger: cheapTroops forKey: @"cheapTroops"];
    [cheapTroops1 synchronize]; 
    NSUserDefaults * raiding1 = [NSUserDefaults standardUserDefaults];
    [raiding1 setInteger: raiding forKey: @"raiding"];
    [raiding1 synchronize]; 
    NSUserDefaults * valiantSoldiers1 = [NSUserDefaults standardUserDefaults];
    [valiantSoldiers1 setInteger: valiantSoldiers forKey: @"valiantSoldiers"];
    [valiantSoldiers1 synchronize];
    NSUserDefaults * cityBarricade1 = [NSUserDefaults standardUserDefaults];
    [cityBarricade1 setInteger: cityBarricade forKey: @"cityBarricade"];
    [cityBarricade1 synchronize]; 
    NSUserDefaults * willBeAttacked1 = [NSUserDefaults standardUserDefaults];
    [willBeAttacked1 setInteger: willBeAttacked forKey: @"willBeAttacked"];
    [willBeAttacked1 synchronize];  
    NSUserDefaults * fighterAmt1 = [NSUserDefaults standardUserDefaults];
    [fighterAmt1 setInteger: fighterAmt forKey: @"fighterAmt"];
    [fighterAmt1 synchronize]; 
    NSUserDefaults * archerAmt1 = [NSUserDefaults standardUserDefaults];
    [archerAmt1 setInteger: archerAmt forKey: @"archerAmt"];
    [archerAmt1 synchronize]; 
    NSUserDefaults * cavalryAmt1 = [NSUserDefaults standardUserDefaults];
    [cavalryAmt1 setInteger: cavalryAmt forKey: @"cavalryAmt"];
    [cavalryAmt1 synchronize]; 
    NSUserDefaults * catapultAmt1 = [NSUserDefaults standardUserDefaults];
    [catapultAmt1 setInteger: catapultAmt forKey: @"catapultAmt"];
    [catapultAmt1 synchronize];
    NSUserDefaults * isNewGame1 = [NSUserDefaults standardUserDefaults];
    [isNewGame1 setInteger: isNewGame forKey: @"isNewGame"];
    [isNewGame1 synchronize];
}

- (IBAction)title1:(id)sender 
{
    title2Controller = [self.storyboard instantiateViewControllerWithIdentifier:@"title2Controller"];
    [self presentModalViewController: title2Controller animated:YES];
    woodW = 4;
    woodS = 8;
    woodI = 10;
    stoneW = 11;
    stoneS = 4;
    stoneI = 7;
    ironW = 7;
    ironS = 10;
    ironI = 5;
    woodLvl = 1;
    stoneLvl = 1;
    ironLvl = 1;
    w = 1;
    s = 1;
    i = 1;
    x = 0;
    z = 0;
    wood = 225;
    stone = 225;
    iron = 225; 
    day = 1;
    fastGathering = FALSE;
    cheapTroops = FALSE;
    raiding = FALSE;
    valiantSoldiers = FALSE;
    cityBarricade = FALSE;
    willBeAttacked = FALSE;
    isNewGame = TRUE;
    declareWar = FALSE;
    offense = FALSE;
    fighterAmt = 0;
    archerAmt = 0;
    cavalryAmt = 0;
    catapultAmt = 0;
    enySoldiers = 0;
    enyArchers = 0;
    enyCavalry = 0;
    enyCatapult = 0;
    allyStrength = 0;
    enemyStrength = 0;
    lostFighters = 0;
    lostArchers = 0;
    lostCavalry = 0;
    lostCatapults = 0;
    victor = 0;
    infoFighter = FALSE;
    infoArcher = FALSE;
    infoCavalry = FALSE;
    infoCatapult = FALSE;
    resourcescroll = FALSE;
    inforesearch1 = FALSE;
    inforesearch2 = FALSE;
    inforesearch3 = FALSE;
    inforesearch4 = FALSE;
    inforesearch5 = FALSE;
    infoAttack1 = FALSE;
    infoAttack2 = FALSE;
}

- (IBAction)title2:(id)sender 
{
    title3Controller = [self.storyboard instantiateViewControllerWithIdentifier: @"title3Controller"];
    [self presentModalViewController: title3Controller animated:YES];
}

- (IBAction)attack:(id)sender 
{
    if (infoAttack1 == FALSE)
    {
        attackInfo.hidden = FALSE;
        attackScroll.hidden = FALSE;
        attackButton.hidden = FALSE;
        infoAttack1 = TRUE;
        attackInfo.text = @"Are you sure? Raiding puts you in combat with a smaller city. If you fail, you could lose some or all of your troops. However, a successful raid will give you bonus resources to further improve your town. Make sure your army is capable of attacking before you do.";
    }
    else
    {
    raidButton.hidden = TRUE;
    offense = TRUE;
    combatController = [self.storyboard instantiateViewControllerWithIdentifier:@"combatController"];
    [self presentModalViewController: combatController animated:YES];
    }
}

- (IBAction)declareWar:(id)sender 
{
    if (infoAttack2 == FALSE)
    {
        attackInfo.hidden = FALSE;
        attackScroll.hidden = FALSE;
        attackButton.hidden = FALSE;
        infoAttack2 = TRUE;
        attackInfo.text = @"Are you sure? Declaring war puts you in combat with another major city. If you fail, you could lose some or all of your troops. However, you must defeat all 10 enemy cities by declaring war on them. Make sure your army is capable of attacking before you do.";
    }
    else
    {
    warButton.hidden = TRUE;
    declareWar = TRUE;
    offense = TRUE;
    combatController = [self.storyboard instantiateViewControllerWithIdentifier:@"combatController"];
    [self presentModalViewController: combatController animated:YES];
    }
}

- (IBAction)title3:(id)sender 
{
    controller = [self.storyboard instantiateViewControllerWithIdentifier:@"controller"];
    [self presentModalViewController: controller animated:YES];
}

- (IBAction)loadGame:(id)sender 
{
    woodW = [[NSUserDefaults standardUserDefaults] integerForKey:@"woodW"];
    woodS = [[NSUserDefaults standardUserDefaults] integerForKey:@"woodS"];
    woodI = [[NSUserDefaults standardUserDefaults] integerForKey:@"woodI"];
    stoneW = [[NSUserDefaults standardUserDefaults] integerForKey:@"stoneW"];
    stoneS = [[NSUserDefaults standardUserDefaults] integerForKey:@"stoneS"];
    stoneI = [[NSUserDefaults standardUserDefaults] integerForKey:@"stoneI"];
    ironW = [[NSUserDefaults standardUserDefaults] integerForKey:@"ironW"];
    ironS = [[NSUserDefaults standardUserDefaults] integerForKey:@"ironS"];
    ironI = [[NSUserDefaults standardUserDefaults] integerForKey:@"ironI"];
    woodLvl = [[NSUserDefaults standardUserDefaults] integerForKey:@"woodLvl"];
    stoneLvl = [[NSUserDefaults standardUserDefaults] integerForKey:@"stoneLvl"];
    ironLvl = [[NSUserDefaults standardUserDefaults] integerForKey:@"ironLvl"];
    w = [[NSUserDefaults standardUserDefaults] integerForKey:@"w"];
    s = [[NSUserDefaults standardUserDefaults] integerForKey:@"s"];
    i = [[NSUserDefaults standardUserDefaults] integerForKey:@"i"];
    x = [[NSUserDefaults standardUserDefaults] integerForKey:@"x"];
    z = [[NSUserDefaults standardUserDefaults] integerForKey:@"z"];
    victor = [[NSUserDefaults standardUserDefaults] integerForKey:@"victor"];
    wood = [[NSUserDefaults standardUserDefaults] integerForKey:@"wood"];
    stone = [[NSUserDefaults standardUserDefaults] integerForKey:@"stone"];
    iron = [[NSUserDefaults standardUserDefaults] integerForKey:@"iron"];
    day = [[NSUserDefaults standardUserDefaults] integerForKey:@"day"];
    fastGathering = [[NSUserDefaults standardUserDefaults] boolForKey:@"fastGathering"];
    cheapTroops = [[NSUserDefaults standardUserDefaults] boolForKey:@"cheapTroops"];
    raiding = [[NSUserDefaults standardUserDefaults] boolForKey:@"raiding"];
    valiantSoldiers = [[NSUserDefaults standardUserDefaults] boolForKey:@"valiantSoldiers"];
    cityBarricade = [[NSUserDefaults standardUserDefaults] boolForKey:@"cityBarricade"];
    willBeAttacked = [[NSUserDefaults standardUserDefaults] boolForKey:@"willBeAttacked"];
    fighterAmt = [[NSUserDefaults standardUserDefaults] integerForKey:@"fighterAmt"];
    archerAmt = [[NSUserDefaults standardUserDefaults] integerForKey:@"archerAmt"];
    cavalryAmt = [[NSUserDefaults standardUserDefaults] integerForKey:@"cavalryAmt"];
    catapultAmt = [[NSUserDefaults standardUserDefaults] integerForKey:@"catapultAmt"];
    isNewGame = FALSE;
    enySoldiers = 0;
    enyArchers = 0;
    enyCavalry = 0;
    enyCatapult = 0;
    allyStrength = 0;
    enemyStrength = 0;
    lostFighters = 0;
    lostArchers = 0;
    lostCavalry = 0;
    lostCatapults = 0;
    victor = 0;
    declareWar = FALSE;
    offense = FALSE;
    infoFighter = FALSE;
    infoArcher = FALSE;
    infoCavalry = FALSE;
    infoCatapult = FALSE;
    resourcescroll = FALSE;
    inforesearch1 = FALSE;
    inforesearch2 = FALSE;
    inforesearch3 = FALSE;
    inforesearch4 = FALSE;
    inforesearch5 = FALSE;
    infoAttack1 = FALSE;
    infoAttack2 = FALSE;
    
    controller = [self.storyboard instantiateViewControllerWithIdentifier:@"controller"];
    [self presentModalViewController: controller animated: YES];
}
- (void)dealloc {
    [dayLabel release];
    [warning release];
    [woodCount release];
    [stoneCount release];
    [ironCount release];
    [victories release];
    [raidButton release];
    [warButton release];
    [attackInfo release];
    [attackScroll release];
    [attackButton release];
    [super dealloc];
}
- (IBAction)attackCheck:(id)sender 
{
    attackInfo.hidden = TRUE;
    attackScroll.hidden = TRUE;
    attackButton.hidden = TRUE;
    warning.hidden = TRUE;
}
@end
